import { Indicator } from "mint-ui"
import axios from 'axios'
import store from '@/store'
import router from '@/router'
export default {
    common: {
        method: 'Get',
        data:{},
        params: {},
        headers:{},
    },
    $axios( options={} ) {
        options = { ...this.common, ...options }
        //请求前显示加载
        Indicator.open('加载中');

        //是否是登录状态
        if (options.headers.token) {
            options.headers.token = store.state.user.token;
            if (!options.headers.token) {
                router.push('/login')
            }
        }

        return axios(options).then(v => {
            let data = v.data.data;

            //如果发送请求中，发现token过期，请求终止，跳转登录
            if (data.code == 1000) {
                Indicator.close();
                localStorage.removeItem('teaUserInfo');
                router.push('/login');
                location.reload();
                return;
            }

            return new Promise((res, rej) => {
                if (!v) return rej();
                //结束请求，关闭加载中
                setTimeout(() => {
                    Indicator.close();
                },500)
                res(data);
            })
        })
    }
}